﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DblogStudio.DAL.Models;

namespace DblogStudio.BLL
{
    public partial class BizLichBaoTri
    {
        public static List<LichBaoTri> GetList(string maChiTietMay)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.LichBaoTriDbSet.Where(e => e.MaChiTietMay == maChiTietMay).OrderBy(e => e.NgayBaoTri).ToList();
            }
        }
        public static List<LichBaoTri> GetListTheoMay(string maMay)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.LichBaoTriDbSet.Where(e => e.ChiTietMay.BoPhanMay.MaySanXuat.MaMaySanXuat == maMay).OrderBy(e => e.NgayBaoTri).ToList();
            }
        }

        public static List<LichBaoTriCustom> GetList(int distance, string manv)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                DateTime temp = DateTime.Now.AddDays(distance);
                DateTime dt = new DateTime(temp.Year, temp.Month, temp.Day, 23, 59, 59);
                var lsCTMDPC = db.PhanCongBaoTriDbSet.Where(e => e.MaNhanVien == manv).Select(e => e.MaChiTietMay).ToList();
                var ls = db.LichBaoTriDbSet.
                    Include("ChiTietMay").
                    Include("ChiTietMay.BoPhanMay").
                    Include("ChiTietMay.BoPhanMay.MaySanXuat").
                    Include("ChiTietMay.BoPhanMay.MaySanXuat.CongDoanSanXuat")
                    .Where(e =>
                        e.NgayBaoTri <= dt &&
                        (string.IsNullOrEmpty(manv) ? true : lsCTMDPC.Contains(e.MaChiTietMay)))
                    .OrderBy(e => e.NgayBaoTri)
                    .Select(e => new LichBaoTriCustom
                    {
                        _TenBoPhanMay = e.ChiTietMay.BoPhanMay.TenBoPhanMay,
                        _TenChiTietMay = e.ChiTietMay.TenChiTietMay,
                        _TenCongDoan = e.ChiTietMay.BoPhanMay.MaySanXuat.CongDoanSanXuat.TenCongDoanSanXuat,
                        _TenMaySanXuat = e.ChiTietMay.BoPhanMay.MaySanXuat.TenMaySanXuat,
                        ChuKy = e.ChuKy,
                        GhiChu = e.GhiChu,
                        MaChiTietMay = e.MaChiTietMay,
                        MaLichBaoTri = e.MaLichBaoTri,
                        MaNhiemVuBaoTri = e.MaNhiemVuBaoTri,
                        NgayBaoTri = e.NgayBaoTri,
                    })
                    .ToList();
                return ls;
            }
        }

        public static LichBaoTri GetByID(string maLichBaoTri)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.LichBaoTriDbSet.Where(e => e.MaLichBaoTri == maLichBaoTri).FirstOrDefault();
            }
        }

        public static bool IsExistByNhiemVuAndChiTietMay(string maNhiemVu, string maChiTietMay)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.LichBaoTriDbSet.Where(e => e.MaNhiemVuBaoTri == maNhiemVu && e.MaChiTietMay == maChiTietMay).FirstOrDefault() != null ? true : false;
            }
        }

        public static CODE_RESULT_RETURN Add(LichBaoTri obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                if (IsExistByNhiemVuAndChiTietMay(obj.MaNhiemVuBaoTri, obj.MaChiTietMay))
                    return CODE_RESULT_RETURN.TenTrung;
                obj.MaLichBaoTri = Guid.NewGuid().ToString();
                try
                {
                    db.LichBaoTriDbSet.Add(obj);
                    db.SaveChanges();
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData<LichBaoTri>(nhatKy, null, obj.MaLichBaoTri, LOAIDOITUONG.LỊCH_BẢO_TRÌ, LOAITHAOTAC.Thêm);
                    BizNhatKyHoatDong.Add(nhatKy, obj);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                    return CODE_RESULT_RETURN.ThatBai;
                }
            }
        }

        public static CODE_RESULT_RETURN Update(LichBaoTri obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var may = db.LichBaoTriDbSet.Where(e => e.MaLichBaoTri == obj.MaLichBaoTri).FirstOrDefault();
                if (may != null)
                {
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData(nhatKy, may, may.MaLichBaoTri, LOAIDOITUONG.LỊCH_BẢO_TRÌ, LOAITHAOTAC.Chỉnh_sửa);
                    may.ChuKy = obj.ChuKy;
                    may.NgayBaoTri = obj.NgayBaoTri;
                    may.GhiChu = obj.GhiChu;
                    db.SaveChanges();
                    BizNhatKyHoatDong.Add(nhatKy, may);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                else
                {
                    throw new Exception("Đối tượng này không tồn tại");
                }
            }
        }
        public static CODE_RESULT_RETURN Update(LichBaoTriCustom obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var may = db.LichBaoTriDbSet.Where(e => e.MaLichBaoTri == obj.MaLichBaoTri).FirstOrDefault();
                if (may != null)
                {
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData(nhatKy, may, may.MaLichBaoTri, LOAIDOITUONG.LỊCH_BẢO_TRÌ, LOAITHAOTAC.Chỉnh_sửa);
                    may.ChuKy = obj.ChuKy;
                    may.NgayBaoTri = obj.NgayBaoTri;
                    may.GhiChu = obj.GhiChu;
                    db.SaveChanges();
                    BizNhatKyHoatDong.Add(nhatKy, may);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                else
                {
                    throw new Exception("Đối tượng này không tồn tại");
                }
            }
        }

        public static CODE_RESULT_RETURN Delete(string maLichBaoTri)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var may = db.LichBaoTriDbSet.Where(e => e.MaLichBaoTri == maLichBaoTri).FirstOrDefault();
                db.LichBaoTriDbSet.Remove(may);
                db.SaveChanges();
                NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                BizNhatKyHoatDong.InitData<LichBaoTri>(nhatKy, may, may.MaLichBaoTri, LOAIDOITUONG.LỊCH_BẢO_TRÌ, LOAITHAOTAC.Xóa);
                BizNhatKyHoatDong.Add<NhiemVuBaoTri>(nhatKy, null);
                return CODE_RESULT_RETURN.ThanhCong;
            }
        }
    }

}
